/////////////////////////////////////////////////
//	File:		BitmapFont.cpp
//	Class:		SGP
//	Updated:	3/6/2012
//	Author:		Roc Myers
//	Purpose:	Class for displaying Bitmap Fonts
/////////////////////////////////////////////////
#include "BitmapFont.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "tinyxml\tinyxml.h"

CBitmapFont::CBitmapFont(void)
{
	m_nCharHeight	= 0;
	m_nCharWidth	= 0;
	m_nPrevWidth	= 0;
	m_nImageID		= -1;
}


CBitmapFont::~CBitmapFont(void)
{
}

void CBitmapFont::Initialize(int nFontID, const char* szFileName)
{
	m_nImageID		= nFontID;
	LoadFontData(szFileName);
}

void CBitmapFont::Print(const char* szText, int nX, int nY, float fScale,int r,int g, int b)
{
	int nColStart = nX;
	int nLen = strlen(szText);

	for(int i = 0; i < nLen; i++)
	{
		char prev;
		char ch = szText[i];
		m_nCharHeight = CharRects[ch].bottom - CharRects[ch].top;
		m_nCharWidth = CharRects[ch].right - CharRects[ch].left;
		if(i == 0)
			m_nPrevWidth = m_nCharWidth;
		else
		{
			prev = szText[i-1];
			m_nPrevWidth = CharRects[prev].bottom - CharRects[prev].top;
		}

		if(ch == ' ')
		{
			nX += m_nPrevWidth * (int)fScale;
			continue;
		}
		else if(ch == '\n')
		{
			nX = nColStart;
			nY += (m_nCharHeight * (int)fScale) + 25;
			continue;
		}

		RECT rTile = CharRects[ch];

		CSGD_TextureManager::GetInstance()->Draw(m_nImageID, nX + (int)(fScale *XOffsets[ch]), nY + (int)(fScale * YOffsets[ch]), fScale, fScale, &rTile,0.0f,0.0f,0.0f,D3DCOLOR_ARGB(255,r,g,b));

		nX += m_nCharWidth * (int)fScale;
	}
}

bool CBitmapFont::LoadFontData(const char* szFileName)
{
	int count;
	int x;
	int y;
	int height;
	int width;
	int id;
	int offsetY;
	int offsetX;

	TiXmlDocument doc;
	if(doc.LoadFile(szFileName) == false)
		return false;

	TiXmlElement* pRoot = doc.RootElement();
	if(pRoot == nullptr)
		return false;

	TiXmlElement* pChars = pRoot->FirstChildElement("chars");
	if(pChars == nullptr)
		return false;

	if(pChars->Attribute("count", &count) == nullptr)
		return false;

	TiXmlElement* pChar = pChars->FirstChildElement("char");
	if(pChar == nullptr)
		return false;

	for(int i = 0; i < count; i++)
	{
		if(pChar->Attribute("id", &id) == nullptr)
			return false;

		if(pChar->Attribute("x", &x) == nullptr)
			return false;

		if(pChar->Attribute("y", &y) == nullptr)
			return false;

		if(pChar->Attribute("width", &width) == nullptr)
			return false;

		if(pChar->Attribute("height", &height) == nullptr)
			return false;

		if(pChar->Attribute("yoffset", &offsetY) == nullptr)
			return false;

		if(pChar->Attribute("xoffset", &offsetX) == nullptr)
			return false;

		CharRects[id].left = x;
		CharRects[id].top = y;
		CharRects[id].right = x + width;
		CharRects[id].bottom = y + height;
		YOffsets[id] = offsetY;
		XOffsets[id] = offsetX;

		if(pChar->NextSiblingElement("char") != nullptr)
			pChar = pChar->NextSiblingElement();
		else
			return false;
	}
	return true;
}
